<?php
/**
 * Created by PhpStorm.
 * User: 户学
 * Date: 2018/4/18
 * Time: 10:34
 */

class Register extends M_Controller
{
    public function index(){
        $USER = $this->get_cache('user');
        //来路认证
        $json = 0;
        $auth = $this->input->get('auth');
        if($auth){
            $auth = md5(SYS_KEY) && exit($this->callback_json(
                array(
                    'msg'=>'授权认证码不正确',
                    'code'=>0
                )
                ));
            !IS_POST && exit($this->callback_json(
                array(
                    'msg'=>'请用POST方式提交',
                    'code'=>0
                )

            ));
            $json = 1;
            $USER['setting']['regcode'] = 0;
        }
        $this->template->assign(array(
            'code' => $USER['setting']['regcode'],
            'back_url' => '',
            'regfield' => $USER['setting']['regfield'],
            'meta_title' => fc_lang('用户注册'),
        ));
        $this->template->display('../gw/register.html');
    }


    //验证码对比
    public function check_mobile_code(){
        //echo "aa";exit;     //用此种方法可先验证是否资料已传到class中
        $phone = $this->input->get_post('mobile');
        $code = $this->input->get_post('code');
        $code_type = $this->input->get_post('code_type');

        $where = array();
        $where['phone'] = $phone;
        $where['is_last'] = 1;

        if(empty($phone)){
            exit;
        }
        if(empty($code)){
            exit;
        }

        $result = $this->db->where($where)->order_by('id desc')->get('sms_code')->row_array();
        //print_r($result);
        $arr = array();
        if($result['code'] == $code){
            $arr['status'] = 1;
            echo json_encode($arr);exit;
        }else{
            $arr['status'] = 0;
            echo json_encode($arr);exit;
        }

    }

    /**
     *  完善用户信息
    */

    public function register_info(){
        $USER = $this->get_cache('user');

        if(IS_POST){
//            $data = $this->input->get_post();
            $name = $this->input->get_post('name');
            $code = $this->input->get_post('code');
            $phone = $this->input->get_post('mobile');
            $grade = $this->input->get_post('grade');
            $subject = $this->input->get_post('subject');
            $back_url = $_POST['back'] ? urlencode($this->input->post('back',true)):'';
            $back_url = $back_url && strpos($back_url,'register') === FALSE ? $back_url : $back_url.dr_user_url('home/index');//判断注册路径
            if( $USER['setting']['regcode'] && !$this->check_captcha('code')){
                $error = array('name'=> 'code','msg'=>fc_lang('验证码不正确'));
            }else {
                $data = array();
                $this->load->model('user_model');
                $salt = substr(md5(rand(0, 999)), 0, 10); // 随机10位密码加密码
                //检查验证码是否正确
                $where = array();
                $where['phone'] = $phone;
                $where['code'] = $code;
                $where['is_last'] = 1;
                //检验验证码是否有效
                $sms = $this->db->where($where)->get('sms_code')->row_array();
                if(!$sms){
                    $this->user_msg(fc_lang('验证码错误'), dr_user_url('register/index'), 1, 1);
                    exit;
                }

                //发送短信验证码
                $passwords = md5($phone);
                $pass = substr($passwords,4,6);
                sendSms($phone,$pass,2);
                $data['salt'] = $salt;
                $data['pas'] = $pass;
                $data['pass'] = md5($pass);
                $data['mobile'] = $phone;
                $data['code'] = $code;
                $data['name'] = $name;
                $data['grade'] = $grade;
                $data['subject'] = $subject;
                $data['addtime'] = date('Y-m-d H:i:s');
                $id = $this->user_model->register($data);
                if($id >0){
                    // 注册成功
                    $data['uid'] = $id;
                    $this->hooks->call_hook('member_register_after', $data); // 注册之后挂钩点
                    //注册短信验证码失去效果
                    $pa = array();
                    $pa['phone'] = $data['mobile'];
                    $pa['code'] = $data['code'];
                    $pa['type'] = 1;
                    $this->db->where($pa)->update('sms_code',array('is_last'=>0));
                    //EC接入数据
                    $ecArr = array("f_name"=>$name,"f_mobile"=>$phone,"81157087"=> $grade,"81157089"=>$subject,"optUserId"=>"7173319","81157096"=>'注册用户',
                        "customFieldMapping"=>array(
                            "81157087"=>array("option"=>'',"type"=>"1"),
                            "81157089"=>array("option"=>'',"type"=>'1'),
                            "81157096"=>array("option"=>'',"type"=>'4')
                        ));
                    $apps = array("appId"=>APP_ID, "appSecret"=>APP_SECRET);
                    $arrs  = ec_getToken(EC_GET_TOKEN,json_encode($apps));
                    $tok = json_decode($arrs,true);
                    $token = $tok['data']['accessToken'];
                    ec_getData(EC_REGISTER_URL, json_encode($ecArr),array("Authorization:$token", "CORP_ID:".CORP_ID));
                    //ec 结束
                    //classin 接入
                    $datas = array("SID"=>CLASSIN_SID,"safeKey"=>md5(CLASSIN_SAFEKEY.time()),"timeStamp"=>time(),"telephone"=>$phone,"nickname"=>$name,"md5pass"=>$data['pass']);
                    send_posts(CLASSIN_REGISTER_URL,$datas);
                    // classin  结束

                    // 注册后的登录
                    $code = $this->user_model->login($id, $data['pass'], $data['auto'] ? 8640000 : $USER['setting']['loginexpire'], 0, 0);
                    strlen($code) > 3 && $this->hooks->call_hook('member_login', $data); // 登录成功挂钩点
                    exit($this->callback_json(array(
                        'msg' => '注册成功',
                        'code' => 1,
                        'uid' => $id,
                        'return' => dr_user_info($id)
                    )));

                }else if($id === -1){
                    $error =  array("name"=>$data['mobile'],'msg'=>"该手机号码已经注册");
                }else{
                    $error = array('name' => 'username', 'msg' => fc_lang('注册失败'));
                }

            }
             exit($this->callback_json(array(
                'msg' => $error['msg'],
                'code' => 0
            )));
            if (IS_AJAX) {
                $error && exit(dr_json(0, $error['msg']));
                $id > 0 && exit(json_encode(array(
                    'status' => 1,
                    'backurl' => $back_url,
                    'syncurl' => dr_member_sync_url($code))));
            }
            $code && $this->member_msg(fc_lang('注册成功').$code, $back_url, 0, 0);
            exit;

        }else{
            $phone = $this->input->get('mobile');
            $code = $this->input->get('code');
            //判断手机号码是否存在
            $user = $this->db->where('mobile',$phone)->get('user')->row_array();
            if($user){
                $this->member_msg(fc_lang('该手机号码已经注册'), dr_user_url('register/index'), 0, 0);
                exit;
            }
            $where = array();
            $where['phone'] = $phone;
            $where['code'] = $code;
            $where['is_last'] = 1;
            //检验验证码是否有效
            $sms = $this->db->where($where)->get('sms_code')->row_array();
            if(!$sms){
                $this->member_msg(fc_lang('验证码错误'), dr_user_url('register/index'), 0, 0);
                exit;
            }
            if($sms && time()-$sms['addtime']>30*60){
                $this->member_msg(fc_lang('验证码已经过期'), dr_user_url('register/index'), 0, 0);
                exit;
            }
            $data = array(
                'mobile'=>$phone,
                'code'=> $code,
            );


        }
        $this->template->assign(array(
            'data' => $data,
            'code' => $USER['setting']['regcode'],
            'back_url' => '',
            'regfield' => $USER['setting']['regfield'],
            'meta_title' => fc_lang('用户注册'),
        ));
        $this->template->display('../gw/register_addinfo.html');
    }
    /**
        检查用户是否存在
     */
    public function check_phone(){
        $phone = $this->input->get_post("phone");
        $data = $this->db->where('mobile',$phone)->get('user')->row_array();
        if($data){
            exit(json_encode(array(
                'status' => 1,
                'msg'=>'手机号码已经存在',)));
        }
        exit(json_encode(array(
            'status' => 0,
            )));

    }

    public function poup_register(){
        $USER = $this->get_cache('user');
        //来路认证
        $json = 0;
        $auth = $this->input->get('auth');
        if($auth){
            $auth = md5(SYS_KEY) && exit($this->callback_json(
                    array(
                        'msg'=>'授权认证码不正确',
                        'code'=>0
                    )
                ));
            !IS_POST && exit($this->callback_json(
                array(
                    'msg'=>'请用POST方式提交',
                    'code'=>0
                )

            ));
            $json = 1;
            $USER['setting']['regcode'] = 0;
        }
        $this->template->assign(array(
            'code' => $USER['setting']['regcode'],
            'back_url' => '',
            'regfield' => $USER['setting']['regfield'],
            'meta_title' => fc_lang('用户注册'),
        ));
        $this->template->display('../gw/poup_register.html');
    }

    public function poup_register_info(){
        $USER = $this->get_cache('user');

        if(IS_POST){
//            $data = $this->input->get_post();
            $name = $this->input->get_post('name');
            $code = $this->input->get_post('code');
            $phone = $this->input->get_post('mobile');
            $grade = $this->input->get_post('grade');
            $subject = $this->input->get_post('subject');
            $back_url = $_POST['back'] ? urlencode($this->input->post('back',true)):'';
            $back_url = $back_url && strpos($back_url,'register') === FALSE ? $back_url : $back_url.dr_user_url('home/index');//判断注册路径
            if( $USER['setting']['regcode'] && !$this->check_captcha('code')){
                $error = array('name'=> 'code','msg'=>fc_lang('验证码不正确'));
            }else {
                $data = array();
                $this->load->model('user_model');
                $salt = substr(md5(rand(0, 999)), 0, 10); // 随机10位密码加密码
                //检查验证码是否正确
                $where = array();
                $where['phone'] = $phone;
                $where['code'] = $code;
                $where['is_last'] = 1;
                //检验验证码是否有效
                $sms = $this->db->where($where)->get('sms_code')->row_array();
                if(!$sms){
                    $this->user_msg(fc_lang('验证码错误'), dr_user_url('register/index'), 1, 1);
                    exit;
                }

                //发送短信验证码
                $passwords = md5($phone);
                $pass = substr($passwords,4,6);
                sendSms($phone,$pass,2);
                $data['salt'] = $salt;
                $data['pas'] = $pass;
                $data['pass'] = md5($pass);
                $data['mobile'] = $phone;
                $data['code'] = $code;
                $data['name'] = $name;
                $data['grade'] = $grade;
                $data['subject'] = $subject;
                $data['addtime'] = date('Y-m-d H:i:s');
                $id = $this->user_model->register($data);
                if($id >0){
                    // 注册成功
                    $data['uid'] = $id;
                    $this->hooks->call_hook('member_register_after', $data); // 注册之后挂钩点
                    //注册短信验证码失去效果
                    $pa = array();
                    $pa['phone'] = $data['mobile'];
                    $pa['code'] = $data['code'];
                    $pa['type'] = 1;
                    $this->db->where($pa)->update('sms_code',array('is_last'=>0));
                    //EC接入数据
                    $ecArr = array("f_name"=>$name,"f_mobile"=>$phone,"81157087"=> $grade,"81157089"=>$subject,"optUserId"=>"7173319","81157096"=>'注册用户',
                        "customFieldMapping"=>array(
                            "81157087"=>array("option"=>'',"type"=>"1"),
                            "81157089"=>array("option"=>'',"type"=>'1'),
                            "81157096"=>array("option"=>'',"type"=>'4')
                        ));
                    $apps = array("appId"=>APP_ID, "appSecret"=>APP_SECRET);
                    $arrs  = ec_getToken(EC_GET_TOKEN,json_encode($apps));
                    $tok = json_decode($arrs,true);
                    $token = $tok['data']['accessToken'];
                    ec_getData(EC_REGISTER_URL, json_encode($ecArr),array("Authorization:$token", "CORP_ID:".CORP_ID));
                    //ec 结束
                    //classin 接入
                    $datas = array("SID"=>CLASSIN_SID,"safeKey"=>md5(CLASSIN_SAFEKEY.time()),"timeStamp"=>time(),"telephone"=>$phone,"nickname"=>$name,"md5pass"=>$data['pass']);
                    send_posts(CLASSIN_REGISTER_URL,$datas);
                    // classin  结束

                    // 注册后的登录
                    $code = $this->user_model->login($id, $data['pass'], $data['auto'] ? 8640000 : $USER['setting']['loginexpire'], 0, 0);
                    strlen($code) > 3 && $this->hooks->call_hook('member_login', $data); // 登录成功挂钩点
                    exit($this->callback_json(array(
                        'msg' => '注册成功',
                        'code' => 1,
                        'uid' => $id,
                        'return' => dr_user_info($id)
                    )));

                }else if($id === -1){
                    $error =  array("name"=>$data['mobile'],'msg'=>"该手机号码已经注册");
                }else{
                    $error = array('name' => 'username', 'msg' => fc_lang('注册失败'));
                }

            }
            exit($this->callback_json(array(
                'msg' => $error['msg'],
                'code' => 0
            )));
            if (IS_AJAX) {
                $error && exit(dr_json(0, $error['msg']));
                $id > 0 && exit(json_encode(array(
                    'status' => 1,
                    'backurl' => $back_url,
                    'syncurl' => dr_member_sync_url($code))));
            }
            $code && $this->member_msg(fc_lang('注册成功').$code, $back_url, 0, 0);
            exit;

        }else{
            $phone = $this->input->get('mobile');
            $code = $this->input->get('code');
            //判断手机号码是否存在
            $user = $this->db->where('mobile',$phone)->get('user')->row_array();
            if($user){
                $this->member_msg(fc_lang('该手机号码已经注册'), dr_user_url('register/index'), 0, 0);
                exit;
            }
            $where = array();
            $where['phone'] = $phone;
            $where['code'] = $code;
            $where['is_last'] = 1;
            //检验验证码是否有效
            $sms = $this->db->where($where)->get('sms_code')->row_array();
            if(!$sms){
                $this->member_msg(fc_lang('验证码错误'), dr_user_url('register/index'), 0, 0);
                exit;
            }
            if($sms && time()-$sms['addtime']>30*60){
                $this->member_msg(fc_lang('验证码已经过期'), dr_user_url('register/index'), 0, 0);
                exit;
            }
            $data = array(
                'mobile'=>$phone,
                'code'=> $code,
            );


        }
        $this->template->assign(array(
            'data' => $data,
            'code' => $USER['setting']['regcode'],
            'back_url' => '',
            'regfield' => $USER['setting']['regfield'],
            'meta_title' => fc_lang('用户注册'),
        ));
        $this->template->display('../gw/poup_register_addinfo.html');
    }
}